package com.cloud.code.生产者消费者;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述:
 * 生产者消费者模拟
 *
 * @author 54485
 * @create 2018-09-17 14:30
 */
public class ProduceAndConsumerBySelfTest {

    private static Queue<Integer> integers = new LinkedList<>();
    private static Lock lock = new ReentrantLock();
    private static Condition conditionCon = lock.newCondition();
    private static Condition conditionPro = lock.newCondition();
    private static int i = 1;
    private static int maxSize = 10;

    public static void main(String[] args) {
        Producer producer = new Producer("生产者",integers);
        Producer producer2 = new Producer("生产者",integers);
        Consumer consumer = new Consumer("消费者",integers);
        Consumer consumer2 = new Consumer("消费者",integers);

        producer.start();
        consumer.start();
        producer2.start();
        consumer2.start();
    }


    public static class Producer extends Thread {

        private Queue<Integer> queue;
        String name;


        public Producer(String name, Queue<Integer> queue) {
            super(name);
            this.name = name;
            this.queue = queue;
        }

        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    if (queue.size() == maxSize) {

                        System.out.println("满了，等等");
                        conditionPro.await();

                    }
                    i++;
                    System.out.println("添加了："+i);
                    queue.add(i);
                    conditionCon.signalAll();
                    conditionPro.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    public static class Consumer extends Thread {

        private Queue<Integer> queue;
        String name;

        public Consumer(String name, Queue<Integer> queue) {
            super(name);
            this.name = name;
            this.queue = queue;
        }

        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    if (queue.isEmpty()) {
                        System.out.println("没了，等等");
                        conditionCon.await();
                    }
                    Integer remove = queue.remove();
                    System.out.println("用了："+remove);


                    conditionCon.signalAll();
                    conditionPro.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }



}
